#include "threadPool.h"
#include "worker.h"
#include "path.h"
#include "puts_gets.h"
#include "printSyslog.h"
int parsingCommand(int netfd,pathStack_t* ppathStack,char* path){
    cmd_t cmd;
    train_t train;
    int flag;
    int ret=recv(netfd,&train,sizeof(train_t),0);
    printf("ret=%d\n",ret);
    printf("%d\n",train.flag);

    memcpy(&cmd,train.data,sizeof(cmd));
    printf("type=%d\n",cmd.cmdType);
    if(train.flag == ISCOMMAND){
        if(cmd.cmdType == ISCD){
            printf("im cd %d\n",ISCD);
            changeDir(&cmd,ppathStack,netfd,path);
        }

        if(cmd.cmdType == ISLS){
            printf("I am ls = %d\n",ISLS);
            listFile(ppathStack,netfd,path);
        }

       if(cmd.cmdType == ISPWD){

            printf("I am pwd = %d\n",ISPWD);
            printWokringDir(ppathStack,netfd);
        }

      
        if(cmd.cmdType == ISPUTS){
        
            printf("I am puts = %d\n",ISPUTS);
            server_puts(netfd,ppathStack,path);
        }

        if(cmd.cmdType == ISGETS){
            printf("I am gets = %d\n",ISGETS);
            server_gets(&cmd,netfd,ppathStack,path);
        }

        
        if(cmd.cmdType == ISRM){
            printf("I am removeFile = %d\n",ISRM);
            removeFile(&cmd,netfd,path);
        }
        
        if(cmd.cmdType == ISMKDIR){
            printf("I am mkdir %d\n",ISMKDIR);
            makeDir(ppathStack,&cmd,path);
        }
        
        if(cmd.cmdType == ISRMDIR){
            removeDir(&cmd,netfd,path);
        }
    }
    sleep(2);
}


int isFileExite(char* path,int netfd){

    if(0 != access(path,F_OK)){
        train_t ret_info;
        bzero(&ret_info,sizeof(ret_info));
        ret_info.flag = ISRESULT;
        strcpy(ret_info.data,"the file is not exited！");
        ret_info.length = strlen(ret_info.data);
        send(netfd,&ret_info,sizeof(ret_info),0);
        return  1;//文件不存在返回1
        
    }
    return 0;//文件在返回0
}
char * getRealPath(const cmd_t cmd,char path[]){
    bzero(path,256);
    strcat(path,cmd.parameter1);
    return path;
}


int removeFile(cmd_t* pcmd,int netfd,char* ppath){
    PRINT_LOG(NULL, 0, "rm");
    cmd_t cmd = *pcmd;
    printf("cmd.type =%d,cmd.p1=%s，cmd.p2=%s\n",cmd.cmdType,cmd.parameter1,cmd.parameter2);
    char path[256] = {0};
   // getRealPath(cmd,path);
    memcpy(path,ppath,strlen(ppath));
    // getCwd(ppathStack,path+25);
    printf("cur path =%s\n", path);
    chdir(path);
    if(isFileExite(path,netfd)==1){//文件不存在返回1，且向服务端返回错误信息
        return 1;
    }

    int ret =  remove(cmd.parameter1);
    ERROR_CHECK(ret,-1,"removeFile\n");
    return 0;
}

int makeDir(pathStack_t * ppathStack ,cmd_t* pcmd,char* ppath){
    PRINT_LOG(NULL, 0, "mkdir");
    cmd_t cmd = *pcmd;
    printf("cmd.type =%d,cmd.p1=%s，cmd.p2=%s\n",cmd.cmdType,cmd.parameter1,cmd.parameter2);
    char path[256] = {0};
    getRealPath(cmd,path);


   

    memcpy(path,ppath,strlen(ppath));
    printf("cur path =%s\n", path);
    chdir(path);
    int ret =  mkdir(cmd.parameter1,0777);
    printf("ret =%d\n",ret);
    ERROR_CHECK(ret,-1,"makeDir\n");
    return 0;
}

int recurRemove(char* path){
struct stat dirStat;
    int ret = stat(path,&dirStat);
      ERROR_CHECK(ret,-1,"stat");
     //Todo  给客户端发送小火车
     
      if(S_ISREG(dirStat.st_mode)){
          //普通文件，直接删除                                                                        
         remove(path) ;
      }
      else if(S_ISDIR(dirStat.st_mode)){
  //目录文件则递归删除
          DIR* dirp = opendir(path);
          printf("open %s\n",path);
          struct dirent * prent;
          char up_dir[] = "..";
          char cur_dir[]= ".";
          while( ( prent = readdir(dirp)) !=NULL){
              if((strcmp(up_dir,prent->d_name)==0) ||(strcmp(cur_dir,prent->d_name)==0)){
              //目录项为i.或者..则忽视ii
                  continue;
              }

              char childPath[256] = {0};
              strcat(childPath,path);
              strcat(childPath,"/");
              strcat(childPath,prent->d_name);
              printf("childPath = %s\n",childPath);
              /*
              strcat(childPath,prent->d_name);
              cmd_t childCmd;
              childCmd.cmdType = ISCOMMAND;
              memcpy(childCmd.parameter2,childPath,sizeof(childPath));*/
              recurRemove (childPath);
          }
          closedir(dirp);
          rmdir(path);
          return 0;
}

}
int removeDir(cmd_t* pcmd,int netfd,char* ppath){
    PRINT_LOG(NULL, 0, "rmdir");
    cmd_t cmd = *pcmd;
      printf("cmd.type =%d,cmd.p1=%s，cmd.p2=%s\n",cmd.cmdType,cmd.parameter1,cmd.parameter2);
      char path[256] = {0};
     // getRealPath(cmd,path);

      memcpy(path,ppath,strlen(ppath));

    memcpy(path,ppath,strlen(ppath));

      // getCwd(ppathStack,path+25);
      printf("cur path =%s\n", path);
      chdir(path);
      if(isFileExite(path,netfd)==1){//文件不存在返回1，且向服务端返回错误信息
          return 1;
      }
      printf("dirname=%s=\n",cmd.parameter1);
      recurRemove(cmd.parameter1);
      return 0;
}
